package com.itwxz.system.controller.mobile;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itwxz.common.core.domain.R;
import com.itwxz.common.utils.SecurityUtils;
import com.itwxz.common.utils.StringUtils;
import com.itwxz.system.domain.po.WxzOrderHistory;
import com.itwxz.system.service.IWxzOrderHistoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "维修站手机端历史订单Controller")
@Slf4j
@RestController
@RequestMapping("/mobile/history")
@RequiredArgsConstructor
public class WxzOrderHistoryMobileController {
    private final IWxzOrderHistoryService orderHistoryService;

    @GetMapping("/list/me")
    @Operation(summary = "查看我的历史订单列表接口", description = "查看我的历史订单订单列表")
    public R<Page<WxzOrderHistory>> getListMe(int pageNum, int pageSize, String query) {
        Page<WxzOrderHistory> page = new Page<>(pageNum, pageSize);

        LambdaQueryWrapper<WxzOrderHistory> wrapper = new LambdaQueryWrapper<>();

        wrapper.eq(WxzOrderHistory::getStaffId, SecurityUtils.getUserId())
                .orderByDesc(WxzOrderHistory::getCreateTime);

        if (StringUtils.isNotBlank(query)) {
            wrapper.and(w -> w.like(WxzOrderHistory::getName, query)
                    .or()
                    .like(WxzOrderHistory::getDetails, query));
        }


        orderHistoryService.page(page, wrapper);
        return R.ok(page);
    }
}
